﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace SP.FeatureAdministrationTool.FeatureManagerModule
{
    public class FarmFeatureManager : FeatureManager
    {
        public override void ActivateFeature(Guid featureId)
        {
            SPWebService.AdministrationService.Features.Add(featureId);
        }

        public override void DeactivateFeature(Guid featureId)
        {
            SPWebService.AdministrationService.Features.Remove(featureId);
        }

        public override List<Feature> GetAllFeatures()
        {
            List<Feature> features = new List<Feature>();
            foreach (Feature feature in this.GetFeatures(SPFeatureScope.Farm))
            {
                try
                {
                    feature.Status = SPWebService.AdministrationService.Features[feature.Id] == null ? FeatureActivationStatus.Deactivated : FeatureActivationStatus.Activated;
                    features.Add(feature);
                }
                catch
                {
                    continue;
                }
            }

            return features;
        }
    }
}
